define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'building.room/index' + location.search,
                    add_url: 'building.room/add',
                    edit_url: 'building.room/edit',
                    del_url: 'building.room/del',
                    multi_url: 'building.room/multi',
                    import_url: 'building.room/import',
                    table: 'building_room',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        // {field: 'user_id', title: __('User_id')},
                        {field: 'zone_name', title: __('Zone'), searchList: false, operate:'=',  addclass: 'selectpage', extend: 'data-source="building_zone/index"'},
                        {field: 'build_name', title: __('Build_id'), searchList: false, operate:'=', addclass: 'selectpage', extend: 'data-source="building/index"'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        // {field: 'type', title: __('Type')},
                        {field: 'house_type', title: __('House_type'), operate: 'LIKE'},
                        {field: 'floor_no', title: __('Floor_no')},
                        // {field: 'status', title: __('Status'), searchList: {0:__('Status 0'),1:__('Status 1'),2:__('Status 2'),3:__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'house.status', title: '上架状态', searchList: {1: '已上架',2:'未上架',3:'已出租'}, formatter: Table.api.formatter.status, visible: false},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        // {field: 'original_price', title: __('Original_price'), operate:'BETWEEN'},
                        {field: 'area', title: __('Area'), operate:'BETWEEN'},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'remark', title: __('Remark'), operate: 'LIKE'},
                        // {field: 'is_remind', title: __('Is_remind')},
                        // {field: 'remind_day', title: __('Remind_day')},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                             //操作栏,默认有编辑、删除或排序按钮,可自定义配置buttons来扩展按钮
                        {
                            field: 'operate',
                            width: "150px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'ajax',
                                    title: '复制房源',
                                    classname: 'btn btn-xs btn-success btn-copy btn-ajax',
                                    icon: 'fa fa-copy',
                                    confirm: '确认复制该房源？',
                                    url: 'building.room/copy',
                                    success: function (data, ret) {
                                        // Layer.alert(ret.msg);
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                        $("#table").bootstrapTable('refresh',{silent:true});
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $("form").on("reset", function () {
                var form = this
                setTimeout(function () {
                    $(form).find('button[type="submit"]').trigger('click')
                }, 10)
            });
        },
        add: function () {
            
             $(document).on('click', '.btn-yibiao', function() {
                
                var type = $(this).data('type');
                var name = $(this).data('name');
                
                console.log('yibiao--',type,name)
                
                var yibiaojson = $("textarea[name='row[yibiaojson]']").val();
                console.log('yibiaojson--',yibiaojson);
                var tmpJson = [];
                if(yibiaojson !='' && yibiaojson != '[]'){
                    var jsonObject = $.parseJSON(yibiaojson);
                    console.log(jsonObject)
                    jsonObject.push({"type":type,"name":name,"price":'',"min_num":'',"last_num":'',"sn":'',"remark":''});
                    tmpJson = jsonObject;
                }else{
                     tmpJson = [{"type":type,"name":name,"price":'',"min_num":'',"last_num":'',"sn":'',"remark":''}];
                }
                
                var tmpJsonStr = JSON.stringify(tmpJson);
                var domhtml = $(".yibiaoclass").html();
                $(".yibiaoclass").empty();
                $(".yibiaoclass").html(domhtml);
                $(".yibiaoclass").find(".form-inline").remove();
                $("textarea[name='row[yibiaojson]']").val(tmpJsonStr);
                Form.events.fieldlist($(".yibiaoclass"));
               
            });   
            
            
             $(document).on('click', '.btn-fixed', function() {
                
                var type_name = $(this).data('type_name');
                var price = $(this).data('price');
                var pay_type = $(this).data('pay_type');
                
                console.log('fixed--',type_name,price,pay_type)
                
                
                var fixedjson = $("textarea[name='row[fixedjson]']").val();
                console.log('fixedjson--',fixedjson);
                var tmpJson = [];
                if(fixedjson !='' && fixedjson != '[]'){
                    var jsonObject = $.parseJSON(fixedjson);
                    console.log(jsonObject)
                    jsonObject.push({"type_name":type_name,"price":price,"pay_type":pay_type,"first_use":0});
                    tmpJson = jsonObject;
                }else{
                     tmpJson = [{"type_name":type_name,"price":price,"pay_type":pay_type,"first_use":0}];
                }
                
                var tmpJsonStr = JSON.stringify(tmpJson);
                var domhtml = $(".fixedclass").html();
                $(".fixedclass").empty();
                $(".fixedclass").html(domhtml);
                $(".fixedclass").find(".form-inline").remove();
                $("textarea[name='row[fixedjson]']").val(tmpJsonStr);
                Form.events.fieldlist($(".fixedclass"));
               
            });
            
            $(document).on('click', '.btn-pt', function() {
                var name = $(this).data('name');
                var num = $(this).data('num');
                var deposit = $(this).data('deposit');
                
                console.log('pt--',name,num,deposit)
                
                var ptjson = $("textarea[name='row[ptjson]']").val();
                console.log('ptjson--',ptjson);
                var tmpJson = [];
                if(ptjson !='' && ptjson != '[]'){
                    var jsonObject = $.parseJSON(ptjson);
                    console.log(jsonObject)
                    jsonObject.push({"name":name,"num":num,"deposit":deposit});
                    tmpJson = jsonObject;
                }else{
                     tmpJson = [{"name":name,"num":num,"deposit":deposit}];
                }
                
                // var tmpJson = [{"name":name,"num":num,"deposit":deposit}];
                var tmpJsonStr = JSON.stringify(tmpJson);
                var domhtml = $(".ptclass").html();
                $(".ptclass").empty();
                $(".ptclass").html(domhtml);
                $(".ptclass").find(".form-inline").remove();
                $("textarea[name='row[ptjson]']").val(tmpJsonStr);
                Form.events.fieldlist($(".ptclass"));
            });
            
            
            Controller.api.bindevent();
        },
        edit: function () {
            
                    $(document).on('click', '.btn-yibiao', function() {
                
                var type = $(this).data('type');
                var name = $(this).data('name');
                
                console.log('yibiao--',type,name)
                
                var yibiaojson = $("textarea[name='row[yibiaojson]']").val();
                console.log('yibiaojson--',yibiaojson);
                var tmpJson = [];
                if(yibiaojson !='' && yibiaojson != '[]'){
                    var jsonObject = $.parseJSON(yibiaojson);
                    console.log(jsonObject)
                    jsonObject.push({"type":type,"name":name,"price":'',"min_num":'',"last_num":'',"sn":'',"remark":''});
                    tmpJson = jsonObject;
                }else{
                     tmpJson = [{"type":type,"name":name,"price":'',"min_num":'',"last_num":'',"sn":'',"remark":''}];
                }
                
                var tmpJsonStr = JSON.stringify(tmpJson);
                var domhtml = $(".yibiaoclass").html();
                $(".yibiaoclass").empty();
                $(".yibiaoclass").html(domhtml);
                $(".yibiaoclass").find(".form-inline").remove();
                $("textarea[name='row[yibiaojson]']").val(tmpJsonStr);
                Form.events.fieldlist($(".yibiaoclass"));
               
            }); 
            
            $(document).on('click', '.btn-fixed', function() {
                
                var type_name = $(this).data('type_name');
                var price = $(this).data('price');
                var pay_type = $(this).data('pay_type');
                
                console.log('fixed--',type_name,price,pay_type)
                
                
                var fixedjson = $("textarea[name='row[fixedjson]']").val();
                
                
                var tmpJson = [];
                if(fixedjson !='' && fixedjson != '[]'){
                    var jsonObject = $.parseJSON(fixedjson);
                    console.log(jsonObject)
                    jsonObject.push({"type_name":type_name,"price":price,"pay_type":pay_type,"first_use":0});
                    tmpJson = jsonObject;
                }else{
                     tmpJson = [{"type_name":type_name,"price":price,"pay_type":pay_type,"first_use":0}];
                }
                
                // var tmpJson = [{"type_name":type_name,"price":price,"pay_type":pay_type,"first_use":0}];
                var tmpJsonStr = JSON.stringify(tmpJson);
                var domhtml = $(".fixedclass").html();
                $(".fixedclass").empty();
                $(".fixedclass").html(domhtml);
                $(".fixedclass").find(".form-inline").remove();
                $("textarea[name='row[fixedjson]']").val(tmpJsonStr);
                Form.events.fieldlist($(".fixedclass"));
               
            });
            
            $(document).on('click', '.btn-pt', function() {
                var name = $(this).data('name');
                var num = $(this).data('num');
                var deposit = $(this).data('deposit');
                
                console.log('pt--',name,num,deposit)
                
                var tmpJson = [];
                if(ptjson !='' && ptjson != '[]'){
                    var jsonObject = $.parseJSON(ptjson);
                    console.log(jsonObject)
                    jsonObject.push({"name":name,"num":num,"deposit":deposit});
                    tmpJson = jsonObject;
                }else{
                     tmpJson = [{"name":name,"num":num,"deposit":deposit}];
                }
                
                // var tmpJson = [{"name":name,"num":num,"deposit":deposit}];
                var tmpJsonStr = JSON.stringify(tmpJson);
                var domhtml = $(".ptclass").html();
                $(".ptclass").empty();
                $(".ptclass").html(domhtml);
                $(".ptclass").find(".form-inline").remove();
                $("textarea[name='row[ptjson]']").val(tmpJsonStr);
                Form.events.fieldlist($(".ptclass"));
            });
            
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                const buildSelect = $('#c-build_id')
                buildSelect.data('params', function (obj) {
                    return {custom: {zone_id: $('#c-zone_id').val()}}
                })
                $(document).on('change', '#c-zone_id', function () {
                    const val = $(this).val()
                    buildSelect.selectPageClear()
                    buildSelect.selectPageDisabled(!val)
                    buildSelect.selectPageRefresh()
                })

                $(document).on('change', '#c-type', function () {
                    if (parseInt($(this).val()) === 1) {
                        $('#house-not-residence').show()
                        $('#house-residence').hide()
                    } else {
                        $('#house-not-residence').hide()
                        $('#house-residence').show()
                    }
                })

                const houseTypeBtn = $('#c-house_type-btn')
                houseTypeBtn.click(() => {
                    const saveType = $('#c-house-save-type')
                    const houseTypeInput = $('#c-house_type_name')
                    const houseTypeIdInput = $('#c-house_type').parent()
                    if (saveType.val() === 'select') {
                        houseTypeInput.show()
                        houseTypeIdInput.hide()
                        saveType.val('input')
                        houseTypeBtn.text('选择户型')
                    } else {
                        houseTypeIdInput.show()
                        houseTypeInput.hide()
                        saveType.val('select')
                        houseTypeBtn.text('添加户型')
                    }
                })

                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
